#ifndef  RECOVERY_FS_FILE_ABSTRACTFILEOPERATION_H
#define  RECOVERY_FS_FILE_ABSTRACTFILEOPERATION_H


#include <string>
#include <memory>
#include "recovery/ErrnoStatus.h"
#include "recovery/Common.h"


namespace recovery {
namespace fs {
namespace file {

class File;
class FileDescriptor;

class  AbstractFileOperation : public Closable<ErrnoStatus> {
public:
    explicit AbstractFileOperation(const std::string & path, bool opened = false);
    virtual ~AbstractFileOperation();

    const File & file() const {
        return *f;
    }
    const FileDescriptor & fd() const {
        return *_fd;
    }

    ErrnoStatus close() override {
        if(isOpen()){
            ErrnoStatus r = closeOnce();
            opened = false;
            return r;
        }
        return ErrnoStatus::OK();
    }
    inline bool isOpen() const{
        return opened;
    }

protected:
    FileDescriptor & writableFd() {
        return *_fd;
    }

    virtual ErrnoStatus closeOnce() = 0;
private:
    std::unique_ptr<File> f;
    std::unique_ptr<FileDescriptor> _fd;
    bool opened;
};

} //end of namespace file
} //end of namespace fs
} //end of namespace recovery

#endif   /* RECOVERY_FS_FILE_ABSTRACTFILEOPERATION_H */
